import tkinter as tk
from tkinter import *
from tkinter import messagebox
import redis
import pymysql
from PIL import ImageTk,Image
import mainpage
import show_grade
import testMysql


class loginpage:
    def __init__(self,master):
        root = master
        root.geometry("300x180")
        root.title("登录")
        page = tk.Frame(root)  # 创建一页纸
        page.pack()
        username = tk.StringVar()
        password = tk.StringVar()
        tk.Label(page).grid(row=0, column=0)
        tk.Label(page, text="账号").grid(row=1, column=1, pady=10)
        tk.Entry(page, textvariable=username).grid(row=1, column=2)
        tk.Label(page, text="密码").grid(row=2, column=1, pady=10)
        tk.Entry(page, textvariable=password).grid(row=2, column=2)

        def login():
            a = username.get()
            b = password.get()
            c=testMysql.DB.testRedis(a)
            if c==b:
                messagebox.showinfo("Message", "登录成功")
                page.destroy()
                mainpage.Mainpage(root)
            else:
                messagebox.showinfo("Message", "登录失败，请重新输入")
        def studentlogin():
            a = username.get()
            b = password.get()
            DBHOST = 'localhost'
            DBUSER = 'root'
            DBPASS = '123456'
            DBNAME = 'test'
            db = pymysql.connect(host=DBHOST, user=DBUSER, password=DBPASS, database=DBNAME)
            cursor = db.cursor()
            sql = "select * from S;"
            cursor.execute(sql)
            results = cursor.fetchall()
            dict = {}
            for row in results:
                dict.update({row[0]: row[5]})
            TK = dict[a]
            if TK == b:
                messagebox.showinfo("Message", "登录成功")
                list = []
                list = testMysql.DB.search(a, b)
                show_grade.show.show_grades(list)
                print(list)
            else:
                messagebox.showinfo("Message", "登录失败，请重新输入")

        tk.Button(page, text="管理员登录", command=login).grid(row=3, column=2)
        tk.Button(page,text="学生登录",command=studentlogin).grid(row=4,column=2)



if __name__ == '__main__':
    root=tk.Tk()
    loginpage(master=root)
    root.mainloop()  # 一定要写

